home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d1 / fff332.arc / ROOTPATH.C < prev    next >
C/C++ Source or Header  |  1990-04-07  |  4KB  |  103 lines

  1. /*************************************************************************
  2.  * RootPath -- Convert a pathname  argument  to  root  based  cannonical *
  3.  *             form.                                                     *
  4.  * Author:     Don A. Williams                                             *
  5.  *             CompuServ -                                                 *
  6.  *             Genie     - DON-WILL                                         *
  7.  *                                                                         *
  8.  * RootPath determines the current directory,  appends the path argument *
  9.  * (which may affect which disk the current directory is  relative  to), *
  10.  * and  qualifies  "."  and  ".." references.  The result is a complete, *
  11.  * simple, path name with drive specifier.                               *
  12.  *                                                                         *
  13.  * If the relative path the user specifies  does  not  include  a  drive *
  14.  * spec., the default drive will be used as the base. (The default drive *
  15.  * will never be changed.)                                                  *
  16.  *                                                                         *
  17.  *     entry:  RelativePath -- pointer to the pathname to be expanded.   *
  18.  *             FullPath -- must point to a working buffer, see warning.     *
  19.  *                                                                         *
  20.  *     exit:   FullPath -- the full path which results.                     *
  21.  *                                                                         *
  22.  *     return: A pointer to FullPath if OK, NULL if an error occurs.
  23.  *                                                                         *
  24.  *     calls:  getcurdir getdisk                                         *
  25.  *                                                                         *
  26.  *     warning: FullPath  must point to a working buffer large enough to *
  27.  *              hold the longest possible relative  path  argument  plus *
  28.  *              the longest possible current directory path.               *
  29.  *                                                                         *
  30.  * RootPath  was  modeled after the public domain file "rootpath.c" with *
  31.  * fairly extensive "enhancement".  The major enhancement  is  provision *
  32.  * for  relative  paths  such  as  "..\..\here"  which  MS-DOS  does NOT *
  33.  * support.  I found that such  a  construct  would  be  very  handy  in *
  34.  * several  of  my uses of UFIND so I added it.  Provision has also been *
  35.  * made to handle either of the two element name  separator  characters; *
  36.  * the '\' of MS-DOS or the '/' of Unix.  The MS-DOS facilities actually *
  37.  * recognize  either  separator  at  the programmatic level but too many *
  38.  * MS-DOS programs do NOT.                                                 *
  39.  *                                                                         *
  40.  ************************************************************************/
  41.  
  42. #include <stdio.h>
  43. #include <string.h>
  44. #include <ctype.h>
  45.  
  46. int GetCurrentDirectory (int Disk, char *CurDir);
  47.  
  48.  char *
  49. RootPath (char *CurDir, char *RelPath, char *FullPath) {
  50.  
  51.     char *p, *p1, *p2;
  52.  
  53.     if (RelPath[0] == '\0') {
  54.         FullPath[0] = '\0';
  55.         return(NULL);
  56.         }
  57.     if ( RelPath[1] == ':') {    /* Path contains drive    */
  58.         FullPath[0] = RelPath[0]; FullPath[1] = RelPath[1];
  59.         memmove(RelPath, RelPath+2, strlen(RelPath) + 1);
  60.         }
  61.     else {
  62.         FullPath[0] = CurDir[0]; FullPath[1] = CurDir[1];
  63.         }
  64.     FullPath[2] = '\0';
  65.  
  66.     if (strlen(RelPath) == 2 && *(RelPath+1) == ':' ) strcat(RelPath, "\\");
  67.     if ( (p = strchr("\\/", *RelPath)) != NULL) strcpy(FullPath+2, RelPath);
  68.     else {
  69.         FullPath[2] = '\\';
  70.         if (FullPath[0] != CurDir[0]) {
  71.             if (GetCurrentDirectory( (int) (toupper(*FullPath) - '@'), &FullPath[3]))
  72.                 return(NULL);
  73.             }
  74.         else {
  75.             strcpy(FullPath, CurDir);
  76.             FullPath[strlen(FullPath)-1] = '\0';
  77.             }
  78.         p = RelPath;
  79.         while (1) {
  80.             p1 = strchr(p, '\\');
  81.             if (!strncmp(p, "..", 2)) {
  82.                 if ( (p2 = strrchr(FullPath, '\\')) != NULL) *p2 = '\0';
  83.                 p = p1 + 1;
  84.                 }
  85.             else if (!strncmp(p, ".", 1)) {
  86.                 p = p1 + 1;
  87.                 break;
  88.                 };
  89.             if (p1 == NULL) break;
  90.             }
  91.         if ( (strlen(FullPath) > 3) || (strlen(FullPath) == 2) )
  92.             strcat(FullPath, "\\");
  93.         strcat(FullPath, p);
  94.         }
  95.  
  96.     while ( (p = strchr(FullPath, '/')) != NULL) *p++ = '\\';
  97.  
  98.     if ( (strlen(FullPath) == 3) && (FullPath[2] == '\\') )
  99.         FullPath[2] = '\0';
  100.     return(strlwr(FullPath));
  101.     }
  102.     
  103.